package ozu.project.regions;

import ozu.project.GameController;
import ozu.project.RiskTurkey;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class RegionController extends MouseAdapter{
    private RiskTurkey risk;

    public RegionController(RiskTurkey risk) {
        this.risk = risk;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
        RegionComponent regionComponent = (RegionComponent) e.getSource();
        OzuRegion region = regionComponent.getRegion();
        risk.getAbstractState().onRegionClick(region);
        if (risk.checkGameOver()) {
            gameOver();
        }
    }

    private void gameOver() {
        String[] objButtons = {"restart", "quit"};
        int promptResult = JOptionPane.showOptionDialog(null,
                "Game Over\n" + risk.getRegions()[0].getOwner().getName() + " won!", "Risk Turkey",
                JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null,
                objButtons, objButtons[1]);
        if (promptResult == 0) {
            GameController.restart();
        } else if (promptResult == 1) {
            GameController.quit();
        }
    }
}
